package com.cumt.threeway.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cumt.threeway.R;
import com.cumt.threeway.constant.ThreeWayConstant;
import com.cumt.threeway.util.ThreeWayUtil;
import com.cumt.threeway.view.dialog.EditNickNameDialog;
import com.cumt.threeway.view.widget.CircleImageView;

import java.io.File;

/**
 * Created by 胡晶晶 on 2016/4/28.
 */
public class PersonInfoActivity extends FragmentActivity implements View.OnClickListener {

    private final int REQUEST_CODE_FOR_CAMERA = 1;
    private final int REQUEST_CODE_FOR_GALLERY = 2;
    private File mCurrentPhotoFile = null;// 拍照得到的图片
    private CircleImageView mAvatarImageView;
    private View mParentAvatarView;
    private View mCameraView;
    private ImageView mScreenBlack;
    private String mPath;
    private String mAccount;
    private String mAccountName;
    private TextView mAccountNameTextView;
    private TextView mNickNameTextView;
    private View mCurrentAccountView;
    private ImageView mIvAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personinfo_activity);
        initView();
    }

    private void initView() {
        mAccount = ThreeWayUtil.getInstance().getLoginAccount(this);
        mAccountName = ThreeWayUtil.getInstance().getThirdUserName(this);
        if(!TextUtils.isEmpty(mAccount)) {
            mCurrentPhotoFile = new File(ThreeWayConstant.PATH + mAccount + "head.jpg");
            mPath = ThreeWayConstant.PATH + mAccount + "head.jpg";
        }
        mAvatarImageView = (CircleImageView) findViewById(R.id.iv_head);
        mScreenBlack = (ImageView) findViewById(R.id.iv_screen_background);
        mParentAvatarView = findViewById(R.id.ll_parent_avatar);
        mNickNameTextView = (TextView) findViewById(R.id.tv_nick_name);
        mAccountNameTextView = (TextView) findViewById(R.id.tv_account_name);
        mCurrentAccountView = findViewById(R.id.ll_current_account);
        mIvAccount = (ImageView) findViewById(R.id.iv_account_ico);

        mAvatarImageView.setOnClickListener(this);
        mCurrentAccountView.setOnClickListener(this);
        findViewById(R.id.btn_camera_get).setOnClickListener(this);
        findViewById(R.id.btn_gallery_get).setOnClickListener(this);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.ll_change_head).setOnClickListener(this);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.ll_nickname).setOnClickListener(this);

        if(!TextUtils.isEmpty(mPath)) {
           Bitmap bitmap = ThreeWayUtil.getInstance().createChangedImage(mPath);
            if(bitmap != null) {
                mAvatarImageView.setImageBitmap(bitmap);
            }
        }

        if("QQ".equals(mAccount)) {
            mAccountNameTextView.setText(mAccountName);
            mIvAccount.setImageResource(R.mipmap.btn_login_qq);
        } else if("SinaWeibo".equals(mAccount)) {
            mAccountNameTextView.setText(mAccountName);
            mIvAccount.setImageResource(R.mipmap.btn_login_weibo);
        } else {
            mAccountNameTextView.setText(mAccountName);
            mCurrentAccountView.setVisibility(View.GONE);
        }

        mNickNameTextView.setText(mAccountName);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_FOR_CAMERA:
                    // 获取相机返回的数据，并转换为Bitmap图片格式
                    if (!ThreeWayUtil.getInstance().startPhotoZoom(this, Uri.fromFile(mCurrentPhotoFile))) {
                    if (data == null) {
                        Bitmap bitmap = data.getParcelableExtra("data");
					        /* 将Bitmap设定到ImageView */
                        if (bitmap != null) {
                            String path = ThreeWayConstant.PATH + ThreeWayUtil.getInstance().getLoginAccount(this) + "head.jpg";
                            mAvatarImageView.setImageBitmap(bitmap);
                            ThreeWayUtil.getInstance().savePicture(path, bitmap);
                            }
                        }
                    }
                    break;
                case REQUEST_CODE_FOR_GALLERY:
                    if (data != null) {
                        String imagePath = ThreeWayUtil.getInstance().getRealPathFromURI(this, data.getData());
                        if (TextUtils.isEmpty(imagePath)) {
                            Toast.makeText(this, "未能找到路径", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (!ThreeWayUtil.getInstance().startPhotoZoom(this, Uri.fromFile(new File(imagePath)))) {
                        if (!TextUtils.isEmpty(imagePath) && (imagePath.endsWith("jpg") || imagePath.endsWith("png"))) {
                            File file = new File(imagePath);
                            if (file.exists()) {
                                Bitmap bitmap = ThreeWayUtil.getInstance().createChangedImage(imagePath);
                                if (bitmap != null) {
                                    ThreeWayUtil.getInstance().savePicture(imagePath, bitmap);
                                    mAvatarImageView.setImageBitmap(bitmap);
                                }
                            }
                        }
                        }
                    }
                    break;
                case ThreeWayConstant.REQUEST_CODE_FOR_CROP:
                    // 获取相机返回的数据，并转换为Bitmap图片格式
                    if (data != null) {
                        Bitmap bitmap = data.getParcelableExtra("data");
					/* 将Bitmap设定到ImageView */
                        if (bitmap != null) {
                            String path = ThreeWayConstant.PATH + ThreeWayUtil.getInstance().getLoginAccount(this) + "head.jpg";
                            mAvatarImageView.setImageBitmap(bitmap);
                            ThreeWayUtil.getInstance().savePicture(path, bitmap);
                        }
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_back:
                setResult(-3);
                finish();
                break;
            case R.id.ll_change_head:
                showAddAvatarAppearAni();
                break;
            case R.id.btn_camera_get:
                showAddAvatarDisappearAni();
                try {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));
                    startActivityForResult(intent, REQUEST_CODE_FOR_CAMERA);
                } catch (ActivityNotFoundException e) {

                }
                break;
            case R.id.btn_gallery_get:
                showAddAvatarDisappearAni();
                try {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent, REQUEST_CODE_FOR_GALLERY);
                } catch (ActivityNotFoundException e) {

                }
                break;
            case R.id.btn_cancel:
                showAddAvatarDisappearAni();
                break;
            case R.id.ll_nickname:
                EditNickNameDialog editNickNameDialog = EditNickNameDialog.newInstance();
                editNickNameDialog.show(getSupportFragmentManager(),"editNickNameDialog");
                break;
        }
    }

    public void setUserNickName(String name) {
        mNickNameTextView.setText(name);
    }

    // 下方三个按钮滑出
    public void showAddAvatarAppearAni() {
        mParentAvatarView.setVisibility(View.VISIBLE);
        mScreenBlack.setVisibility(View.VISIBLE);
        mParentAvatarView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_bottom_in));
    }

    // 下方三个按钮滑走
    private void showAddAvatarDisappearAni() {
        mParentAvatarView.clearAnimation();
        mParentAvatarView.setVisibility(View.GONE);
        mScreenBlack.setVisibility(View.GONE);
    }
}
